{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc5d9eee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello World\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9870533a",
   "metadata": {},
   "source": [
    "# 学习Numpy和Matploylib\n",
    "\n",
    "学习网站： https://www.runoob.com/numpy/numpy-tutorial.html\n",
    "\n",
    "## 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c2f9f449",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01155df9",
   "metadata": {},
   "source": [
    "快速运行 Shift + Enter\n",
    "\n",
    "Markdown 和 代码 模式，分别书写文档和代码， 代码运行后会产生结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33446c3c",
   "metadata": {},
   "source": [
    "## NumPy Ndarray 对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b14d9f7f",
   "metadata": {},
   "source": [
    "### 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cfa0acfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 40]\n"
     ]
    }
   ],
   "source": [
    "data = np.array([10, 20, 30, 40])\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "562cf8ea",
   "metadata": {},
   "source": [
    "### 二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9d2a4b65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 3 5 7]\n",
      " [2 4 6 8]]\n"
     ]
    }
   ],
   "source": [
    "data2 = np.array([[1, 3, 5, 7], [2, 4, 6, 8]])\n",
    "print(data2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed9f4403",
   "metadata": {},
   "source": [
    "### 自动填充数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b88f9f14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "[ 1  3  5  7  9 11 13 15 17 19]\n"
     ]
    }
   ],
   "source": [
    "data3 = np.arange(6)   # 0 - 5 \n",
    "print(data3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36ed80ac",
   "metadata": {},
   "source": [
    "`arange(a, b, c)`\n",
    "从a开始到区间 [a, b) 内 按照公差为c进行生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "516c818e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0  -2  -4  -6  -8 -10 -12 -14 -16 -18]\n"
     ]
    }
   ],
   "source": [
    "data4 = np.arange(0, -20, -2) # 0 d=-2  \n",
    "print(data4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07149cb3",
   "metadata": {},
   "source": [
    "### Test \n",
    "1、 二维数组，2行\n",
    "\n",
    "2、 第一行从数字 3 开始到数字 10\n",
    "\n",
    "3、 第二行数字是第一行数字的逆序相反数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b3ed00f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  3   4   5   6   7   8   9  10]\n",
      " [-10  -9  -8  -7  -6  -5  -4  -3]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(3, 11, 1)\n",
    "b = np.arange(-10, -2, 1)\n",
    "c = np.array([a, b])\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0066469f",
   "metadata": {},
   "source": [
    "### shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "eacfb9e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "[[0 1 2]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "data5 = np.arange(6)   # 0 - 5 \n",
    "print(data5)\n",
    "data5.shape = (2, 3)\n",
    "print(data5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb894bbe",
   "metadata": {},
   "source": [
    "### 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fdf860e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    }
   ],
   "source": [
    "data6 = np.arange(18)   # 0 - -17 \n",
    "data6.shape = (3, 6)\n",
    "print(data6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fdec71dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  7 13]\n"
     ]
    }
   ],
   "source": [
    "print(data6[... , 1]) # 第2列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0aa8f01a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "print(data6[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e93bb469",
   "metadata": {},
   "source": [
    "## 数学和统计上的数值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35e6d2b8",
   "metadata": {},
   "source": [
    "平均值， 中位数， 绝对值， 方差， 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c3cec247",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.5\n"
     ]
    }
   ],
   "source": [
    "data6 = np.arange(18)   # 0 - -17 \n",
    "print(np.mean(data6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "91828b9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.5\n"
     ]
    }
   ],
   "source": [
    "print(np.median(data6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "000f6a8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-10  -9  -8  -7  -6  -5  -4  -3]\n",
      "[10  9  8  7  6  5  4  3]\n"
     ]
    }
   ],
   "source": [
    "b = np.arange(-10, -2, 1)\n",
    "print(b)\n",
    "print(np.abs(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a4555128",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "26.916666666666668\n"
     ]
    }
   ],
   "source": [
    "print(np.var(data6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6a6f1637",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.188127472091127\n"
     ]
    }
   ],
   "source": [
    "print(np.std(data6))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c62c20c0",
   "metadata": {},
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c38532a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-45  -9   1   7   8  10  56]\n",
      "56\n",
      "-45\n"
     ]
    }
   ],
   "source": [
    "data7 = np.array([1, -9, 10 , 7 ,8 ,56, -45])\n",
    "print(np.sort(data7))\n",
    "print(np.max(data7))\n",
    "print(np.min(data7))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67d99d99",
   "metadata": {},
   "source": [
    "## 反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "09b95996",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1  -9  10   7   8  56 -45]\n"
     ]
    }
   ],
   "source": [
    "print(data7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "36311428",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-45  56   8   7  10  -9   1]\n"
     ]
    }
   ],
   "source": [
    "data8 = data7[::-1]\n",
    "print(data8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8af6165",
   "metadata": {},
   "source": [
    "## 广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2649c716",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0]\n",
      " [10 20 30]\n",
      " [50 60 70]]\n"
     ]
    }
   ],
   "source": [
    "data_9 = np.array([[0, 0, 0],[10, 20, 30], [50, 60, 70]])\n",
    "print(data_9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5fce8a78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5  5  5]\n",
      " [15 25 35]\n",
      " [55 65 75]]\n"
     ]
    }
   ],
   "source": [
    "print(data_9 + 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "28589ced",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  1  1]\n",
      " [11 21 31]\n",
      " [51 61 71]]\n"
     ]
    }
   ],
   "source": [
    "data10 = np.array([1, 1, 1])\n",
    "print(data_9 + data10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e35471b",
   "metadata": {},
   "source": [
    "## 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d36579e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[11 24]\n",
      " [39 56]]\n"
     ]
    }
   ],
   "source": [
    "data_11 = np.array([[1, 2],[3, 4]])\n",
    "data_12 = np.array([[11, 12],[13, 14]])\n",
    "print(data_11 * data_12) # 对应元素相应相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b11adebe",
   "metadata": {},
   "source": [
    "1 * 11 + 2 * 13 = 11 + 26 = 37\n",
    "1 * 12 + 2 * 14 = 12 + 28 = 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6263e72c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[37 40]\n",
      " [85 92]]\n"
     ]
    }
   ],
   "source": [
    "print(np.dot(data_11, data_12)) # 矩阵的点积"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
